package com.zzl.bonds.ticket.controller.feign;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.bonds.ticket.dto.ApproveLogAddDto;
import com.zzl.bonds.ticket.dto.BusinessContractUpdateDto;
import com.zzl.bonds.ticket.dto.feign.NoticeApprovalResult;
import com.zzl.bonds.ticket.dto.feign.TicketOperateLogAddDto;
import com.zzl.bonds.ticket.dto.finance.SupplierCreditListDto;
import com.zzl.bonds.ticket.dto.finance.TicketFinanceOrderDto;
import com.zzl.bonds.ticket.entity.ApproveLogEntity;
import com.zzl.bonds.ticket.entity.SupplierCreditEntity;
import com.zzl.bonds.ticket.service.*;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.Result;
import com.zzl.general.common.utils.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ticket 服务 对外提供的 接口
 *
 * @author : spp
 * @date : 2021/12/1 14:22
 */
@ApidocController("票据服务 rpc 接口")
@RestController
@RequestMapping("bondsticket/feign")
@Slf4j
public class FeignController extends WebBaseCtrl {

    @Autowired
    @SuppressWarnings("all")
    private SupplierCreditService supplierCreditService;
    @Autowired
    private CoreEnterpriseCreditService coreEnterpriseCreditService;
    @Autowired
    private TicketFinanceService ticketFinanceService;

    @Autowired
    @SuppressWarnings("all")
    private TicketOpenService ticketOpenService;

    @Autowired
    private TicketCirculationEnterpriseService ticketCirculationEnterpriseService;

    @Autowired
    private TicketOperateLogService ticketOperateLogService;
    @SuppressWarnings("all")
    @Autowired
    private BusinessContractService businessContractService;
    @SuppressWarnings("all")
    @Autowired
    private ApproveLogService approveLogService;

    @PostMapping("/approveEndNotice")
    @ApidocOperation(description = "审批流程结束通知", operationId = "fei2134")
    @ApidocReturnPage(genericTypeClass = SupplierCreditEntity.class)
    public Result approveEndNotice(@RequestBody NoticeApprovalResult noticeApprovalResult) {
        if (null == noticeApprovalResult || null == noticeApprovalResult.getApprovalId()
                || null == noticeApprovalResult.getApprovalResult()) {
            return Result.error(ResultCode.ERROR_PARAM);
        }
        try {
            ticketOpenService.approveEndNotice(noticeApprovalResult, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }


    @GetMapping("/getInstitutyCredit")
    @ApidocOperation(description = "集团授信", operationId = "fei211")
    @ApidocReturnPage(genericTypeClass = SupplierCreditEntity.class)
    public Map<String, Double> getInstitutyCredit() {
        return coreEnterpriseCreditService.getAllAmountByInstityId();
    }

    /**
     * 根据企业id 查找授信额度 以及在 各个金融机构的额度
     *
     * @param enterpriseIds 企业id 集合
     * @return 返回map key 是企业id value 是 该企业的 信用额度
     */
    @PostMapping("/getEnterpriseQuota")
    @ApidocOperation(description = "分组查询 供应商的 开票额度", operationId = "fei")
    @ApidocReturnPage(genericTypeClass = SupplierCreditEntity.class)
    public Map<String, List<SupplierCreditEntity>> list(@ApidocParamsPage(name = "params") @RequestBody List<String> enterpriseIds) {
        return supplierCreditService.getEnterpriseQuota(enterpriseIds);
    }

    /**
     * 订单中心使用的接口
     */
    @GetMapping("/getFinanceByTicketNo/{ticketNo}")
    @ApidocOperation(description = "根据融资凭证号融单详情", operationId = "/ticket/getFinanceByTicketNo/ticketNo")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = TicketFinanceOrderDto.class)
    })
    public TicketFinanceOrderDto getFinanceByTicketNo(@ApidocParam(name = "ticketNo", description = "融资凭证编号", required = true) @PathVariable("ticketNo") String ticketNo) {
        return ticketFinanceService.getFinanceByTicketNo(ticketNo);
    }

    @GetMapping("/getFinanceCreditList/{enterpriseId}")
    @ApidocOperation(description = "查询融资额度列表", operationId = "list/1/1")
    @ApidocReturnPage(genericTypeClass = SupplierCreditListDto.class)
    public List<SupplierCreditEntity> getFinanceList(@PathVariable("enterpriseId") String enterpriseId) {
        return supplierCreditService.getFinanceCreditList(enterpriseId.length() > 1 ? this.getChangeEnterpriseUser(enterpriseId) : this.getUser());
    }

    /**
     * 获取该票的可转单企业id
     */
    @GetMapping("/getAcquirerDropDownList/{ticketId}")
    public List<String> getAcquirerDropDownList(@PathVariable("ticketId") String ticketId) {
        return ticketCirculationEnterpriseService.getAcquirerDropDownList(ticketId);
    }

    /**
     * 票据操作日志
     *
     * @return
     */
    @PostMapping(value = "/saveTicketLog")
    public Result saveTicketLog(@RequestBody TicketOperateLogAddDto addDto) {
        try {
            ticketOperateLogService.saveFeignTicketLog(addDto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    @PostMapping("/updateContract")
    @ApidocOperation(description = "修改电子合同表", operationId = "ticket/businesscontract" + "/updateContract")
    @ApidocReturnR
    public Result update(@ApidocParam(description = "修改的电子合同表对象值", name = "businessContract", required = true) @RequestBody BusinessContractUpdateDto businessContract) {
        try {
            businessContractService.updateBusinessContractById(businessContract, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    @PostMapping("/getApproveLogList")
    @ApidocOperation(description = "根据类型查询审核记录数据", operationId = "ticket/approvelog" + "/list")
    @ApidocReturnR
    public Result getApproveLogList(@RequestBody ApproveLogEntity approveLog) {
        return Result.ok().data(approveLogService.getApproveLogList(approveLog));
    }
    @PostMapping("/saveApproveLog")
    @ApidocOperation(description = "保存审核记录", operationId = "ticket/approvelog" + "/save")
    @ApidocReturnR
    public Result save(@ApidocParam(description = "保存的审核记录对象值", name = "approveLog", required = true) @RequestBody ApproveLogAddDto approveLog) {
        try {
            approveLogService.saveApproveLog(approveLog, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 保存
     */
    @PostMapping("/approveLogSave")
    @ApidocOperation(description = "保存审核记录", operationId = "ticket/approvelog" + "/save")
    @ApidocReturnR
    public Result approveLogSave(@ApidocParam(description = "保存的审核记录对象值", name = "approveLog", required = true) @RequestBody ApproveLogAddDto approveLog) {
        try {
            approveLogService.saveApproveLog(approveLog, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 列表
     */
    @GetMapping("/list/{businessId}")
    @ApidocOperation(description = "查询审核记录", operationId = "ticket/approvelog" + "/list")
    @ApidocReturnPage(genericTypeClass = ApproveLogEntity.class)
    public List<ApproveLogEntity> list(@ApidocParam(description = "审核记录id", name = "id", required = true) @PathVariable("businessId") String businessId) {
        Map<String,Object> query=new HashMap<>();
        query.put("business_id",businessId);
        query.put("approve_type",1);
        return  new ArrayList<>(approveLogService.listByMap(query));

    }
}
